// pages/department/info/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        department: {},
        all: false,
        schedules: [],
        day: ""
    },

    checkboxChange(e) {
        console.log(e)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let id = options.department_id
        let timer = new Date()
        const year = timer.getFullYear()
        const month = timer.getMonth() + 1 // 由于月份从0开始，因此需加1
        const day = timer.getDate()
        let d = `${this.pad(year, 4)}-${this.pad(month)}-${this.pad(day)}`
        this.setData({
            day: d
        })
        this.getDepartmentById(id)
        this.getScheduleList(id, this.data.all, d)
    },
    pad(timeEl, total = 2, str = '0') {
        return timeEl.toString().padStart(total, str)
    },
    mydata(e) { //可获取日历点击事件
        let data = e.detail.data
        console.log(data)
        this.setData({
            day: data
        })
        this.getScheduleList(this.data.department.id, this.data.all, this.data.day)
    },
    getDepartmentById(id) {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "GET",
            showLoading: true,
            url: `/api/department/` + id,
            message: "正在获取...",
            // data:_this.data.card,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                _this.setData({
                    department: res.data.data
                })
                wx.setNavigationBarTitle({
                    title: _this.data.department.dept_name //页面切换，更换页面标题
                })
            }
        }).catch(err => {
            console.log(err)
        });
    },
    getScheduleList(departmentId, only, day) {
        var _this = this
        let timer = new Date(day)
        console.log(timer)

        let now = new Date()
        const year = now.getFullYear()
        const month = now.getMonth() + 1 // 由于月份从0开始，因此需加1
        const nowDay = now.getDate()
        let d = `${_this.pad(year, 4)}-${_this.pad(month)}-${_this.pad(nowDay)}`
        now = new Date(d)
        let week = timer.getDay()
        //发送请求
        if (now.getTime() <= timer.getTime()) {
            httpUtils.request({
                method: "POST",
                showLoading: true,
                url: `/api/schedule/list`,
                message: "正在获取...",
                data: {
                    department_id: parseInt(departmentId),
                    only: only,
                    day: day,
                    user_id: app.globalData.userInfo.id,
                    week: week
                },
            }).then(res => {
                if (res.data.code != 0) {
                    ui.showToast(res.data.mes)
                } else {
                    for (let i = 0; res.data.data != null && i < res.data.data.length; i++) {
                        res.data.data[i].doctor.info = JSON.parse(res.data.data[i].doctor.introduce)
                    }
                    _this.setData({
                        schedules: res.data.data
                    })
                    console.log(res.data.data)
                }
            }).catch(err => {
                console.log(err)
            });
        }

    },
    checkboxChange(e) {
        let tempAll = false
        if (e.detail.value.length != 0) {
            tempAll = true;
        }
        this.setData({
            all: tempAll
        })
        this.getScheduleList(this.data.department.id + '', this.data.all, this.data.day)
    },
    toOrder(e) {
        console.log(e)
        wx.navigateTo({
            url: '../order/index?ticket_id=' + e.currentTarget.dataset.params + '&dept_name=' + this.data.department.dept_name,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})